import xml.etree.ElementTree as ET
import json
import os
import sys


# precise_repo_xts = "third_party/skia"
precise_repo_xts = sys.argv[1]
DEEP = int(sys.argv[2]) if len(sys.argv) > 2 else -1
home = os.path.dirname(os.path.dirname(os.path.dirname(os.getcwd())))
acts_home = os.getcwd()
manifest_home = os.path.join(home , ".repo")
preloader_path = os.path.join(home,"out","preloader")
precise_gni_name = "precise_run.gni"
depends_tree_name = "depends_tree.gni"

# 首层依赖关系dist
dependency_dist = {}
# 组件依赖记录
bundleList_dist = {}
# 

def get_f_dependes_dist (path):
    count = 0
    # 遍历目录下的所有文件和子目录
    for root, dirs, files in os.walk(path):
        for file in files:
            if file == 'bundle.json':
                count = count + 1
                # 构建完整的文件路径
                file_path = os.path.join(root, file)
                # 读取 JSON 文件并转换为 Python 对象
                with open(file_path, 'r', encoding='utf-8') as f:
                    json_data = json.load(f)
                    bundle_name = json_data['name'].split('/')[-1]
                    if "deps" not in json_data['component']:
                        continue
                    deps = json_data['component']['deps']
                    # 依赖关系额写入dependency_dist
                    if bundle_name not in dependency_dist:
                        dependency_dist[bundle_name] = []
                    if "components" in deps:
                        components_defs = deps['components']
                        for c_def in components_defs:
                            if c_def in dependency_dist:
                                dependency_dist[c_def].append(bundle_name)
                            else:
                                dependency_dist[c_def] = [bundle_name]
                    if "third_party" in deps:
                        third_party_defs = deps['third_party']
                        for t_def in third_party_defs:
                            if t_def in dependency_dist:
                                dependency_dist[t_def].append(bundle_name)
                            else:
                                dependency_dist[t_def] = [bundle_name]
    print(count)


def getbundnamelist ():
    # 遍历XML文件内容
    manifest_xml_list1 = []
    for child in ET.parse(manifest_home + "manifest.xml").getroot().findall('include'):
        manifest_xml_list1 += [manifest_home+"manifests/"+child.attrib['name']]
    print (manifest_xml_list1)
    manifest_xml_list2 = []
    path_list = []
    for xml in manifest_xml_list1:
        root = ET.parse(xml).getroot()
        if root.find('include') is not None:
            print ("in include")
            for child in root.findall('include'):
                manifest_xml_list2.append(manifest_home + "manifests/" + child.attrib['name'])
        else:
            print ("in else")
            for child in ET.parse(xml).getroot().findall('project'):

                if child.attrib['name'] in precise_repo_xts_list and len(precise_repo_xts_list) != 0:
                    path_list.append(child.attrib['path'] + "/bundle.json")
                    precise_repo_xts_list.remove(child.attrib['name'])
    print (manifest_xml_list2)
    print (path_list)
    if len(manifest_xml_list2) != 0 and  len(precise_repo_xts_list) != 0:
        for xml in manifest_xml_list2:
            for child in ET.parse(xml).getroot().findall('project'):
                if child.attrib['name'] in precise_repo_xts_list and len(precise_repo_xts_list) != 0:
                    path_list.append(child.attrib['path'] + "/bundle.json")
                    precise_repo_xts_list.remove(child.attrib['name'])
    print (path_list)
    bundle_list = []
    for path in path_list:
        # 读取JSON文件并获取指定key的value
        with open(home+path, 'r') as file:
            data = json.load(file)
        bundle_list.append(data['component']['name'])
    return bundle_list




if __name__ == '__main__':
    # 计算首层依赖
    get_f_dependes_dist(home)
    # 获取全部仓名
